package com.framework.shs.a.controller;
/**
 * 
 */
import java.io.IOException;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import redis.clients.jedis.Jedis;

import com.framework.shs.a.entity.User;
import com.framework.shs.a.service.UserService;
import com.framework.shs.commom.base.BaseController;
import com.framework.shs.commom.config.Global;
import com.framework.shs.commom.servlet.ValidateCodeServlet;
import com.framework.shs.commom.util.CacheUtils;
import com.framework.shs.commom.util.JedisUtils;
import com.framework.shs.commom.util.SolrUtils;
import com.framework.shs.commom.util.StringUtils;
import com.framework.shs.commom.util.UserUtils;

@Controller
@RequestMapping("${adminPath}/")
public class SystemController extends BaseController {

	@Autowired
	private UserService userService;

	/**
	 * 登陆的转向
	 *
	 * @time : 2016-6-3
	 * @author : sharker
	 *
	 */
	@RequestMapping("toLogin")
	public String login(User user) {
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			return "redirect:" + Global.getAdminPath() + "/main";
		}
		return "login";
	}

	/**
	 * 登陆后的主页位置
	 *
	 * @time : 2016-6-3
	 * @author : sharker
	 * @throws IOException 
	 * @throws SolrServerException 
	 * @throws Throwable 
	 *
	 */
	@RequestMapping("main")
	public String main(User user) throws SolrServerException, IOException, Throwable {
		//User u = (User) CacheUtils.get("user");
		return "index";
	}

	/**
	 * 简单的登陆环节
	 *
	 * @time : 2016-6-3
	 * @author : sharker
	 *
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String isLogin(User currUser, HttpServletRequest request,String valicode,RedirectAttributes attributes) {
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			return "redirect:" + Global.getAdminPath() + "/main";
		}
		
		String code = (String)request.getSession().getAttribute(ValidateCodeServlet.VALIDATE_CODE);
		if(!Global.getConfig("dev.model").equals("true")){
			if(StringUtils.isBlank(code)||!code.equalsIgnoreCase(valicode)){
				attributes.addFlashAttribute("message","验证码错误");
				return "redirect:" + Global.getAdminPath() + "/toLogin";
			}
		}
		Subject user = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(
				currUser.getLoginName(), currUser.getPassword());
		if(StringUtils.isNotBlank(currUser.getMemberMe())&&currUser.getMemberMe().equals("yes")){
			 token.setRememberMe(true);
		}
		try {
			user.login(token);
			if (user.isAuthenticated()) {
				return "redirect:" + Global.getAdminPath() + "/main";
			}
		} catch (AuthenticationException e) {
			token.clear();
			attributes.addFlashAttribute("message","账号密码错误");
			return "redirect:" + Global.getAdminPath() + "/toLogin";
		}
		return "redirect:" + Global.getAdminPath() + "/toLogin";
	}

	/**
	 * 登出的简易环节
	 * @time 2016-6-3
	 * @author : sharker
	 *
	 */
	@RequestMapping("logout")
	public String logout() {
		User user = UserUtils.getUser();
		System.out.println(user);
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect:" + Global.getAdminPath() + "/toLogin";
	}

}
